using Gtk;
using System;
using System.Collections.Generic;

namespace Banshee.Plugins.iTunesImporter
{
	internal class SummaryWidget : Gtk.Bin
	{
		private Label RatingsLabel, StatsLabel, PlaylistsLabel, SPlaylistsLabel,
			PartialTitle, FailedTitle, PartialMessage, FailedMessage,
			NothingLabel, InterruptedLabel;
		private Expander PartialExpander, FailedExpander;
		
		public delegate void Del();
		public Del UnmapDel;
		public uint Ratings, Stats, Playlists, SPlaylists;
		public List<SmartPlaylist> Partial, Failed;
		public bool Interrupted;
		protected Gtk.ScrolledWindow ScrollWindow;
				
		private void MakeLabel(Label label, uint val, string singular, string plural)
		{
			if(val > 0) {
				label.LabelProp = "<big><big>" + val.ToString() + " ";
				label.LabelProp += (val == 1) ? singular : plural;
				label.LabelProp += " imported</big></big>";
			} else {
				label.Destroy();
			}
		}
		
		public void Initialize()
		{
			Stetic.Gui.Build(this, typeof(Banshee.Plugins.iTunesImporter.SummaryWidget));
			Viewport vp = ScrollWindow.Child as Viewport;
			vp.ModifyBg(StateType.Normal, new Gdk.Color(0xfa,0xf9,0xf7));
			if (!Interrupted) {
				InterruptedLabel.Destroy();
			}
			if (Ratings > 0 || Stats > 0 || Playlists > 0 || SPlaylists > 0 ||
				Partial.Count > 0 || Failed.Count > 0 || Interrupted) {
				NothingLabel.Destroy();
			}
			MakeLabel(RatingsLabel, Ratings, "rating", "ratings");
			MakeLabel(StatsLabel, Stats, "play statistic", "play statistics");
			MakeLabel(PlaylistsLabel, Playlists, "playlist", "playlists");
			string fully = (Partial.Count > 0) ? " fully" : "";
			MakeLabel(SPlaylistsLabel, SPlaylists, "smart playlist" + fully, "smart playlists" + fully);
			if(Partial.Count > 0) {
				PartialTitle.LabelProp = "<big><big>" + Partial.Count + " smart ";
				PartialTitle.LabelProp += (Partial.Count == 1)
					? "playlist"
					: "playlists";
				PartialTitle.LabelProp += " partially imported</big></big>";
				foreach(SmartPlaylist sp in Partial) {
					PartialMessage.LabelProp += "<b>" + sp.Name + "</b>\n" +
					"<small><i>could not resolve the following criteria:</i>\n" +
					sp.Ignore + "\n\n<i>but did resolve the following criteria:</i>\n" +
					sp.Output + "</small>\n\n";
				}
			} else {
				PartialExpander.Destroy();
			}
			if(Failed.Count > 0) {
				FailedTitle.LabelProp = "<big><big>" + Failed.Count + " smart ";
				FailedTitle.LabelProp += (Failed.Count == 1)
					? "playlist"
					: "playlists";
				FailedTitle.LabelProp += " failed to import</big></big>";
				foreach(SmartPlaylist sp in Failed)
				{
					FailedMessage.LabelProp += "<b>" + sp.Name + "</b>\n";
				}
			} else {
				FailedExpander.Destroy();
			}
		}

		protected virtual void CloseClicked(object sender, System.EventArgs e)
		{
			if (UnmapDel != null) {
				UnmapDel();
			}
		}
	}
}
